const Service = require('egg').Service;
const queueKey = 'REDIS_DISTRIBUTE_TASK_ORIGIN' // redis大队列
class PushEventService extends Service {
  /**
   *
   * @param {*} tmp
   */
  async buy(tmp, splitStr, isInvitation) {
    let {
      ctx,
      app
    } = this
    let {
      common
    } = ctx.service

    let {
      orderId
    } = tmp;
    // console.log('---redis---', app.redis.lpush)
    let orderDetail = await common.findOne({
      sql: `
      SELECT *
      FROM hd_user_order
      WHERE orderid = ${orderId}

    `})

    // if (orderDetail) { // 订单已经存在
    //   ctx.logger.info(`订单已存在`)
    //   ctx.body = 'ok'
    //   return true
    // }
    // 订单写入数据库
    if (!orderDetail) {
      let param = {
        userid: tmp.userid,
        mobile: `'${tmp.mobile}'`,
        orderid: tmp.orderId,
        productid: tmp.productId,
        pay_type: tmp.payType,
        product_name: `'${tmp.productName}'`,
        money: tmp.money,
        product_type: tmp.productType,
        pay_money: tmp.payMoney,
        coupon_type: tmp.couponType,
        coupon_money: tmp.couponMoney,
        platform: tmp.platform,
        combination_id: splitStr[0],
        activity_id: parseInt(splitStr[1]),
        commodityid: splitStr[2] || 0,
        push_event_id: tmp.event_id,
        order_time: `'${tmp.datetime}'`
      }
      await common.insertOne('hd_user_order', param);

    } else if (!isInvitation) {
      ctx.logger.info(`订单已存在`)
      ctx.body = 'ok'
      return false
    }
    let str = splitStr[0] + '-' + tmp.userid + '-' + tmp.money + '-' + tmp.productType + '-' + tmp.productId + '-' + splitStr[1] + '-' + splitStr[2] + '-' + tmp.orderId + '-' + splitStr[3]
    if (str) {
      await app.redis.lpush(queueKey, str)
    }
  }
}

module.exports = PushEventService